class Applicant < ActiveRecord::Base
  has_many :purchases
  
  belongs_to :department
  belongs_to :role

  #根据申购人编号和姓名检索申购人列表
  def self.findApplicants applicant_number,applicant_name,state,pageBegin,pageEnd
    find(:all,:conditions => ['login_id like ? and name like ? and auditing_status like ?','%'+applicant_number+'%','%'+applicant_name+'%','%'+state+'%'],:offset=>pageBegin.to_i,:limit =>pageEnd.to_i)
  end

  def self.countApplicants applicant_number,applicant_name,state
    count(:all,:conditions => ['login_id like ? and name like ? and auditing_status like ?','%'+applicant_number+'%','%'+applicant_name+'%','%'+state+'%'])
  end

  #通过申购人编号查找申购人
  def self.findByNumber number
    find(:first,:conditions => ['login_id = ?',number])
  end

  scope :name_number_like, lambda {  |name,for_cbb|
    if for_cbb == 'false'
      where('name like ? OR login_id like ?', '%'+name+'%','%'+name+'%')
    else
      where('(name like ? OR login_id like ?) AND is_locked = 0', '%'+name+'%','%'+name+'%').select([:id,:name,:login_id])
    end
  }

  scope :paginate, lambda { |start,limit| { :offset => start.to_i,:limit => limit.to_i,:order=>"id DESC" }}
end
